package com.gxa.service.impl;

import com.gxa.common.base.LayuiResultJson;
import com.gxa.common.base.R;
import com.gxa.common.constant.Constants;
import com.gxa.domain.dto.SysRoleSaveDTO;
import com.gxa.domain.entity.SysRole;
import com.gxa.mapper.SysRoleMapper;
import com.gxa.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public LayuiResultJson findSysRoleAll(Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<SysRole> sysUsers = sysRoleMapper.findSysRoleList(offset,rows);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = sysRoleMapper.countSysRole();
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(sysUsers);

        return layuiResultJson;
    }

    @Override
    public R delRoleByRoleId(Integer roleId) {
        int result = sysRoleMapper.delRoleByRoleId(roleId);
        if(result > 0 ){
            return R.success("删除成功！");
        }
        return R.error();
    }

    @Override
    public R addSysRole(SysRoleSaveDTO sysRoleSaveDTO) {
        int result = sysRoleMapper.addSysRole(sysRoleSaveDTO);
        if (result >0){
            return R.success("添加成功");
        }
        return R.error();
    }

    @Override
    public R updateSysRole(SysRole sysRole) {
        int result = sysRoleMapper.updateSysRole(sysRole);
        if (result >0){
            return R.success("修改成功");
        }
        return R.error();
    }

    @Override
    public LayuiResultJson search(String roleName, Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<SysRole> sysRoles = sysRoleMapper.findSysRoleListByCondition(offset,rows,roleName);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = sysRoleMapper.countSysRoleByCondition(roleName);
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(sysRoles);
        return layuiResultJson;
    }
}
